package ag.carnot.pinkblob.web.jsf.beans.pinkblob.finalizedesign;

import ag.carnot.web.jsf.common.structureddata.ComplexTypeWrapper;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;

import org.apache.log4j.Logger;

import pinkblob.log.Log;

public class PreparePresentationBean {

	private static Logger log = new Log()
			.getLogger(PreparePresentationBean.class);

	public void complete() {

		// variables that have to be filled for completion
		String subject = (String) getPresentationInvitation().get("subject");
		String to = (String) getPresentationInvitation().get("to");
		String body = (String) getPresentationInvitation().get("body");

		log.info("subject " + subject + ", to " + to);

		// create message to display
		FacesMessage msg = new FacesMessage();
		String summary = "";
		if (subject == null || subject.trim().equals("")) {
			summary = "You have to select a subject!";
		}
		if (to == null || to.trim().equals("")) {
			summary = summary + " You are required to select a recipient!";
		}
		if (to != null
				&& !to.toUpperCase().matches(
						"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$")) {
			summary = summary
					+ " The recipient you selected is not a valid email adress!";
		}
		if (body == null || body.trim().equals("")) {
			summary = summary + " You are required to send some message!";
		}
		if (!(summary.equals(""))) {
			log.error(summary);
			msg.setSummary(summary);
			throw new ValidatorException(msg);
		}

	}

	private ComplexTypeWrapper projectInformationWrapper = new ComplexTypeWrapper(
			"ProjectInformation", null);

	public ComplexTypeWrapper getProjectInformationWrapper() {
		return projectInformationWrapper;
	}

	@SuppressWarnings("unchecked")
	public Map getProjectInformation() {
		return (Map) projectInformationWrapper.getComplexType();
	}

	@SuppressWarnings("unchecked")
	public void setProjectInformation(Map projectInformation) {
		projectInformationWrapper = new ComplexTypeWrapper(
				projectInformationWrapper.getDataMappingId(),
				projectInformation);
	}

	private ComplexTypeWrapper presentationInvitationWrapper = new ComplexTypeWrapper(
			"PresentationInvitation", null);

	public ComplexTypeWrapper getPresentationInvitationWrapper() {
		return presentationInvitationWrapper;
	}

	@SuppressWarnings("unchecked")
	public Map getPresentationInvitation() {
		return (Map) presentationInvitationWrapper.getComplexType();
	}

	@SuppressWarnings("unchecked")
	public void setPresentationInvitation(Map presentationInvitation) {
		presentationInvitationWrapper = new ComplexTypeWrapper(
				presentationInvitationWrapper.getDataMappingId(),
				presentationInvitation);
	}
}
